package com.suredy.app.porject.ctrl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suredy.app.porject.model.ContractFile;
import com.suredy.app.porject.service.ContractFileSrv;
import com.suredy.app.porject.tools.FileTool;
import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.core.mvc.model.MessageModel;

/**
 * 合同文件管理类
 * 
 * @author VIVID.G
 * @since 2015-10-27
 * @version v0.1
 */
@Controller
@RequestMapping("/contract-file")
public class ContractFileCtrl extends BaseCtrl {

	@Autowired
	private FileTool fileTool;

	@Autowired
	private ContractFileSrv contractFileSrv;

	@RequestMapping("del/{fileId}")
	@ResponseBody
	public Object removeFile(@PathVariable String fileId) {
		if (StringUtils.isBlank(fileId))
			return new MessageModel(false, "无效文件！", null);

		ContractFile file = this.contractFileSrv.get(fileId);

		if (file == null)
			return new MessageModel(false, "未找到对应文件！", null);

		file = this.contractFileSrv.delete(file);

		if (file == null)
			return new MessageModel(false, "删除文件失败！", null);

		boolean success = this.fileTool.removeByRelativelyPath(file.getFilePath());

		return new MessageModel(true, success ? "文件删除成功！" : "数据已清除，但无法删除服务器磁盘中的对应文件！", null);
	}

}
